<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetEvaluationData, PatchFreetrialeval } from '@/api/modules/freetrialapply'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const route = useRoute()
const TrialApplyId = route.query.TrialApplyId

const findFrom = ref({
  TrialApplyId: route.query.TrialApplyId ? route.query.TrialApplyId : '',
  TrialProductId: '',
  UserId: '',
  NickName: '',
  BindPhone: '',
  AuditStatus: '',
  ViewConBase: '',
  UsedViewConSortDesc: '',
  ViewConSortPriority: '',
  PraiseConBase: '',
  UsedPraiseConSortDesc: '',
  PraiseConSortPriority: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.TrialApplyId !== '' && ({ TrialApplyId: findFrom.value.TrialApplyId })),
    ...(findFrom.value.TrialProductId !== '' && ({ TrialProductId: findFrom.value.TrialProductId })),
    ...(findFrom.value.UserId !== '' && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.NickName !== '' && ({ NickName: findFrom.value.NickName })),
    ...(findFrom.value.BindPhone !== '' && ({ BindPhone: findFrom.value.BindPhone })),
    ...(findFrom.value.AuditStatus !== '' && ({ AuditStatus: findFrom.value.AuditStatus })),
    ...(findFrom.value.ViewConBase !== '' && ({ ViewConBase: findFrom.value.ViewConBase })),
    ...(findFrom.value.UsedViewConSortDesc !== '' && ({ UsedViewConSortDesc: findFrom.value.UsedViewConSortDesc })),
    ...(findFrom.value.ViewConSortPriority !== '' && ({ ViewConSortPriority: findFrom.value.ViewConSortPriority })),
    ...(findFrom.value.PraiseConBase !== '' && ({ PraiseConBase: findFrom.value.PraiseConBase })),
    ...(findFrom.value.UsedPraiseConSortDesc !== '' && ({ UsedPraiseConSortDesc: findFrom.value.UsedPraiseConSortDesc })),
    ...(findFrom.value.PraiseConSortPriority !== '' && ({ PraiseConSortPriority: findFrom.value.PraiseConSortPriority })),
  }
  await GetEvaluationData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`获取失败。${dataRes.errors}`)
    }
  })
  loading.value = false
}

// 图片预览
const srcList = ref([])

function handlePreview(row: any) {
  srcList.value = row.pictures.map((url: string) => url)
}

// 导航栏切换
function handleSelect(key: string) {
  findFrom.value.AuditStatus = key
  GetDataList(1, PageSize)
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 审核
const dialogFormVisible = ref(false)
const AuditFrom = ref({
  auditStatus: true,
  id: 0,
  auditReason: '',
})
function Audit(scope: any) {
  dialogFormVisible.value = true
  AuditFrom.value.id = scope.row.id
}

function SortModification() {
  const params = {
    id: AuditFrom.value.id,
    auditStatus: AuditFrom.value.auditStatus,
    auditReason: AuditFrom.value.auditReason,
  }

  if (!AuditFrom.value.auditStatus && AuditFrom.value.auditReason === '') {
    ElMessage.error('请输入审核失败原因')
    return
  }
  PatchFreetrialeval(params).then((dataRes: any) => {
    if (dataRes.succeeded) {
      ElMessage.success('审核成功')
      GetDataList()
    }
    else {
      ElMessage.error('审核失败')
    }
  })
}

// 添加
function AddData() {
  router.push({
    name: 'freetrialEvaluationDetail',
  })
}

// 编辑
function EditData(id: number) {
  router.push({
    name: 'freetrialEvaluationDetail',
    query: {
      id,
    },
  })
}

// 设置
function OpenSet(id: number) {
  router.push({
    name: 'evaluationConfiguration',
    query: {
      id,
    },
  })
}

// 排序
function Sort(column: any) {
  if (column.label === '浏览数量') {
    if (findFrom.value.UsedViewConSortDesc !== '') {
      (findFrom.value.UsedViewConSortDesc as string | boolean) = !findFrom.value.UsedViewConSortDesc
    }
    else {
      (findFrom.value.UsedViewConSortDesc as string | boolean) = true
    }
    GetDataList(1, PageSize)
  }
  else if (column.label === '点赞数量') {
    if (findFrom.value.UsedPraiseConSortDesc !== '') {
      (findFrom.value.UsedPraiseConSortDesc as string | boolean) = !findFrom.value.UsedPraiseConSortDesc
    }
    else {
      (findFrom.value.UsedPraiseConSortDesc as string | boolean) = true
    }
    GetDataList(1, PageSize)
  }
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
}

onMounted(() => {
  GetDataList()
})
</script>

<template>
  <div>
    <el-menu :default-active="findFrom.AuditStatus" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="">
        全部
      </el-menu-item>
      <el-menu-item index="0">
        待审核
      </el-menu-item>
      <el-menu-item index="1">
        审核成功
      </el-menu-item>
      <el-menu-item index="2">
        审核失败
      </el-menu-item>
    </el-menu>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="试用品申请ID">
                <el-input v-model="(findFrom.TrialApplyId as string)" placeholder="请输入试用品申请ID" :disabled="TrialApplyId !== undefined" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="试用商品ID">
                <el-input v-model="findFrom.TrialProductId" placeholder="请输入试用商品ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="昵称">
                <el-input v-model="findFrom.NickName" placeholder="请输入昵称" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="手机号">
                <el-input v-model="findFrom.BindPhone" placeholder="请输入手机号" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="最少浏览数量">
                <el-input v-model="findFrom.ViewConBase" placeholder="请输入最少浏览数量" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="最少点赞数量">
                <el-input v-model="findFrom.PraiseConBase" placeholder="请输入最少点赞数量" @keyup.enter="Inquire" />
              </el-form-item>
              <!-- <el-form-item label="浏览排序优先级">
                <el-input v-model="findFrom.ViewConSortPriority" style="width: 300px;" placeholder="请输入浏览排序优先级" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="点赞排序优先级">
                <el-input v-model="findFrom.PraiseConSortPriority" style="width: 300px;" placeholder="请输入点赞排序优先级" @keyup.enter="Inquire" />
              </el-form-item> -->
              <!-- <el-form-item label="浏览数量排序">
                <el-select v-model="findFrom.UsedViewConSortDesc" placeholder="请选择浏览数量排序" style="width: 300px;">
                  <el-option label="降序" :value="true" />
                  <el-option label="升序" :value="false" />
                </el-select>
              </el-form-item>
              <el-form-item label="点赞数量排序">
                <el-select v-model="findFrom.UsedPraiseConSortDesc" placeholder="请选择点赞数量排序" style="width: 300px;">
                  <el-option label="降序" :value="true" />
                  <el-option label="升序" :value="false" />
                </el-select>
              </el-form-item> -->
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border @header-click="Sort">
        <el-table-column align="center" prop="id" label="ID" width="80" />
        <el-table-column align="center" prop="trialApplyId" label="试用品申请ID" width="150" />
        <el-table-column align="center" prop="productInfo.productId" label="商品ID" width="80" />
        <el-table-column align="center" label="商品封面" width="100">
          <template #default="scope">
            <!-- <ElImage style=" width: auto;height: 80px;" :src="imgDomain + scope.row.productInfo?.cdnProductPhotoPic" /> -->
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.productInfo?.cdnProductPhotoPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productInfo.productId" label="商品名称" width="250">
          <template #default="scope">
            <span style="margin-right: 10px;">
              {{ scope.row.productInfo?.productName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productInfo.productId" label="商品类型" width="100">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.productInfo?.flag === 1 ? '正常商品'
                : scope.row.productInfo?.flag === 2 ? '积分商品'
                  : scope.row.productInfo?.flag === 3 ? '秒杀商品' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" prop="productInfo.sort" label="商品排序" width="100" /> -->
        <el-table-column align="center" prop="subscriberInfo.userId" label="用户ID" width="100">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <span>
              {{ scope.row.subscriberInfo?.userId }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="subscriberInfo.scOpenId" label="OpenID" width="320">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <span>
              {{ scope.row.subscriberInfo?.scOpenId }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="subscriberInfo" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriberInfo?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriberInfo?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriberInfo?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="scoreOverall" label="推荐指数" width="100" />
        <el-table-column align="center" prop="scoreItem1" label="肤感评分" width="100" />
        <el-table-column align="center" prop="scoreItem2" label="吸收评分" width="100" />
        <el-table-column align="center" prop="scoreItem3" label="保湿评分" width="100" />
        <el-table-column align="center" prop="scoreItem4" label="滋润评分" width="100" />
        <el-table-column align="center" prop="scoreItem5" label="香气评分" width="100" />
        <el-table-column header-align="center" prop="contents" label="测评文本内容" width="550" />
        <el-table-column align="center" prop="buyVipPic" label="评测图片" width="500">
          <template #default="scope">
            <el-scrollbar>
              <div class="scrollbar-flex-content">
                <el-image
                  v-for="(item, index) in scope.row.pictures"
                  :key="index"
                  class="scrollbar-flex-img"
                  :src="item"
                  :preview-src-list="srcList"
                  :initial-index="index"
                  :preview-teleported="true"
                  @click="handlePreview(scope.row)"
                />
              </div>
            </el-scrollbar>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="auditStatus" label="审核状态" width="100">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.auditStatus === 0 ? '待审核'
                : scope.row.auditStatus === 1 ? '审核成功'
                  : scope.row.auditStatus === 2 ? '审核失败' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="auditTime" label="审核时间" width="200" />
        <el-table-column align="center" prop="viewCon" label="浏览数量" fixed="right" width="100" />
        <el-table-column align="center" prop="praiseCon" label="点赞数量" fixed="right" width="100" />
        <el-table-column align="center" prop="addTime" label="发布时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="修改时间" width="200" />
        <el-table-column align="center" label="操作" fixed="right" width="130">
          <template #default="scope">
            <div class="button-box">
              <el-button v-if="scope.row.auditStatus === 0" @click="Audit(scope)">
                审核
              </el-button>
              <span v-else style="margin-right: 10px;">
                已审核
              </span>
              <el-button type="primary" @click="EditData(scope.row.id)">
                编辑
              </el-button>
              <el-button type="primary" @click="OpenSet(scope.row.id)">
                设置
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
    <el-dialog v-model="dialogFormVisible" title="手动审核" width="500">
      <el-form :model="AuditFrom">
        <el-form-item>
          <el-radio-group v-model="AuditFrom.auditStatus">
            <el-radio :value="true" size="large">
              成功
            </el-radio>
            <el-radio :value="false" size="large">
              失败
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="AuditFrom.auditStatus === false" label="审核失败原因">
          <el-input v-model="AuditFrom.auditReason" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">
            取消
          </el-button>
          <el-button type="primary" @click="SortModification">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 80px;
    margin-right: 10px;
  }
}

.button-box {
  display: flex;
  flex-direction: column;
  align-items: center;

  button {
    margin-top: 10px;
    margin-left: 0;
  }
}
</style>
